package achi.community.irla.database;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet(name = "DatabaseServlet", urlPatterns = {"/DatabaseServlet"})
public class DatabaseServlet extends HttpServlet {

    @EJB
    DatabaseImpl db;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.close();
        /**
        String action = request.getParameter("action");
        if (action != null) {

            if (action.equals("add")) {
                String name = request.getParameter("name");
                String cat = request.getParameter("cat");
                Double price = Double.parseDouble(request.getParameter("price"));
                Product p = new Product(name, cat, price);
           //     db.addProduct(p);
            } else if (action.equals("edit_get")) {
                int id = Integer.parseInt(request.getParameter("id"));
           //     Product p = db.find(id);
         //       String xml = productToXml(p);
            //    out.print(xml);
                out.close();
            }
            else if (action.equals("edit_post")) {
                Long id = Long.parseLong(request.getParameter("id"));
                String name = request.getParameter("name");
                String cat = request.getParameter("cat");
                Double price = Double.parseDouble(request.getParameter("price"));

                Product p = new Product(name, cat, price);
                p.setId(id);
             //   db.updateProduct(p);
            } else if (action.equals("delete")) {
                int id = Integer.parseInt(request.getParameter("id"));
             //   db.remove(id);
            }
        } // Get all product if no action is set
        else {
            String html = getProducts();
            out.print(html);
            out.close();
        }
        */
    }

    private String getProducts() {
        String prodTable = "<table border='0' cellspacing='5'>"
                + "<tr><td>Id</td><td>Name</td><td>Cat</td><td colspan='3'>Price</td></tr>";
/**
        List<Product> productlist = db.findAll();

        for (Product p : productlist) {
            prodTable += "<tr><td>";
            prodTable += p.getId();
            prodTable += "</td><td>";
            prodTable += p.getName();
            prodTable += "</td><td>";
            prodTable += p.getCategory();
            prodTable += "</td><td>";
            prodTable += p.getPrice();
            prodTable += "</td><td>";
            prodTable += "<input class='btnEdit' id='" + p.getId() + "' value='edit' type='submit' />";
            prodTable += "</td><td>";
            prodTable += "<input class='btnDelete' id='" + p.getId() + "' value='delete' type='submit' />";
            prodTable += "</td></tr>";
        }
        prodTable += "</table>";
*/
        return prodTable;
    }

    /**
    private String productToXml(Product p) {
        return "<product><id>" + p.getId() + "</id>"
                + "<name>" + p.getName() + "</name>"
                + "<cat>" + p.getCategory() + "</cat>"
                + "<price>" + p.getPrice() + "</price></product>";
    }
    */

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
